package com.hand.wfive.activity.picker

import android.content.Context
import android.content.Intent
import android.widget.FrameLayout
import com.hand.wfive.R
import com.hand.wfive.entity.Params
import es.voghdev.pdfviewpager.library.PDFViewPager
import es.voghdev.pdfviewpager.library.adapter.PDFPagerAdapter
import kotlinx.android.synthetic.main.activity_picker_preview_pdf.*

class PreviewPdfActivity : BasePreviewActivity() {

    companion object {
        fun showIntent(context: Context, path: String): Intent {
            val intent = Intent(context, PreviewPdfActivity::class.java)
            intent.putExtra(Params.path, path)
            return intent
        }
    }

    private lateinit var mPDFViewPager: PDFViewPager

    override fun getContentViewId() = R.layout.activity_picker_preview_pdf

    override fun onPreviewFile() {
        mPDFViewPager = PDFViewPager(mContext, mPath)
        fl_pdf_preview.addView(
            mPDFViewPager, FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT
            )
        )
    }

    override fun getUploadFileType() = Params.fileTypePdf

    override fun onDestroy() {
        super.onDestroy()
        if (this::mPDFViewPager.isInitialized) {
            val adapter = mPDFViewPager.adapter
            if (adapter is PDFPagerAdapter) adapter.close()
        }
    }
}